package com.sz.mes.service.impl.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.vo.JsonResult;
import com.sz.mes.dto.warehouse.ShelvesDTO;
import com.sz.mes.entity.warehouse.Lattice;
import com.sz.mes.entity.warehouse.LatticeGoods;
import com.sz.mes.entity.warehouse.Shelves;
import com.sz.mes.mapper.warehouse.AreaMapper;
import com.sz.mes.mapper.warehouse.LatticeGoodsMapper;
import com.sz.mes.mapper.warehouse.LatticeMapper;
import com.sz.mes.mapper.warehouse.ShelvesMapper;
import com.sz.mes.service.warehouse.ShelvesService;
import com.sz.mes.service.impl.BaseCustomServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 货区货架相关Service实现
 *
 * @author hem
 * @version 1.0
 * @date 2023-05-25
 * Copyright © szhs
 */
@Service
@Slf4j
public class ShelvesServiceImpl extends BaseCustomServiceImpl<ShelvesMapper, Shelves> implements ShelvesService {
    private final LatticeMapper latticeMapper;
    private final ShelvesMapper shelvesMapper;
    private final LatticeGoodsMapper latticeGoodsMapper;
    private final AreaMapper areaMapper;

    public ShelvesServiceImpl(LatticeMapper latticeMapper, ShelvesMapper shelvesMapper,
                              LatticeGoodsMapper latticeGoodsMapper,AreaMapper areaMapper) {
        this.latticeMapper = latticeMapper;
        this.shelvesMapper = shelvesMapper;
        this.latticeGoodsMapper = latticeGoodsMapper;
        this.areaMapper = areaMapper;
    }

    @Override
    public void creatLattice(ShelvesDTO entity) {
        if (entity.getCol() > 0 && entity.getLine() > 0) {
            for (int i = 1; i <= entity.getCol(); i++) {
                for (int j = 1; j <= entity.getLine(); j++) {
                    Lattice lattice = new Lattice();
                    lattice.setWarehouseShelvesId(entity.getId());
                    lattice.setCol(i);
                    lattice.setLine(j);
                    lattice.setLabel(entity.getWarehouseName() + "-" + entity.getWarehouseAreaCode() + "-" + entity.getSlelfNumber()
                            + "-" + i + "-" + j);
                    latticeMapper.insert(lattice);
                }
            }
        }
    }

    //删除货架以及货架格子
    @Override
    public JsonResult delete(Long id) {
        QueryWrapper<Lattice> latticeQueryWrapper = new QueryWrapper<>();
        latticeQueryWrapper.eq("warehouse_shelves_id", id);
        List<Lattice> latticeList = latticeMapper.selectList(latticeQueryWrapper);
        //查询货架上是否存在商品
        for (Lattice lattice : latticeList) {
            QueryWrapper<LatticeGoods> latticeGoodsQueryWrapper = new QueryWrapper<>();
            latticeGoodsQueryWrapper.eq("warehouse_lattice_id", lattice.getId());
            //一个货架可能有多个商品，所以用selectlist
            List<LatticeGoods> latticeGoodsList = latticeGoodsMapper.selectList(latticeGoodsQueryWrapper);
            if (latticeGoodsList.size() > 0) {
                //如果该货架存在商品则，不能删除
                return JsonResult.FAIL_EXCEPTION("货架上还存在商品，请先处理商品");
            }
        }
        //如果能从for走下来，证明货架上全都没有商品，可以删除所有货架
        int deleteBatch = latticeMapper.deleteBatchByList(latticeList);
        //删除货格
        int delete = shelvesMapper.deleteByShelveId(id);
        if (delete > 0 && deleteBatch > 0) {
            return JsonResult.OK();
        } else {
            return JsonResult.FAIL_EXCEPTION("删除失败");
        }
    }
}
